package com.bigknow.appstore.proxy.exceptions.processor.impl;

import com.bigknow.appstore.proxy.enumerate.ErrorType;
import com.bigknow.appstore.proxy.enumerate.ProxyState;
import com.bigknow.appstore.proxy.vo.RequestVo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;

import java.io.Serializable;

/**
 *
 * Created by chaos on 2016/9/23.
 */
public final class ErrorAssert implements Serializable {

    public static boolean isFullHttpRequestMsg(FullHttpMessage msg) {
        if (msg instanceof FullHttpRequest == false) {
            return false;
        } else return true;
    }

    public static boolean isFullHttpResponseMsg(FullHttpMessage msg) {
        if (msg instanceof FullHttpResponse == false) {
            return false;
        } else return true;
    }

    public static void assertProxyServerState(ChannelHandlerContext ctx, ProxyState proxyState) {
        if (!proxyState.equals(ProxyState.RUN)) {
            throwError(ctx, ErrorType.PORXY_NO_LONGER_SERVE, null);
        }
    }

    public static void assertFullHttpMsg(ChannelHandlerContext ctx, Object msg, RequestVo requestVo, String... content) {
        if (!(msg instanceof FullHttpMessage)) throwError(ctx, ErrorType.HTTP_DATA_ERROR, requestVo);
    }

    public static void assertFullHttpRequestMsg(ChannelHandlerContext ctx, FullHttpMessage msg, RequestVo requestVo, String... content) {
        if (!isFullHttpRequestMsg(msg)) throwError(ctx, ErrorType.REQUEST_DATA_ERROR, requestVo);
    }

    public static void assertFullHttpResponseMsg(ChannelHandlerContext ctx, FullHttpMessage msg, RequestVo requestVo, String... content) {
        if (!isFullHttpResponseMsg(msg)) throwError(ctx, ErrorType.REQUEST_DATA_ERROR, requestVo);
    }

    public static void assertUriHasRequestVo(ChannelHandlerContext ctx, RequestVo requestVo) {
        if (requestVo == null) {
            throwError(ctx, ErrorType.URI_NO_PASS, null);
        } else if (requestVo.isErrorCall()) {
            throwError(ctx, ErrorType.URI_NO_PASS, requestVo);
        }
    }


    public static void throwError(ChannelHandlerContext ctx, ErrorType errorType, RequestVo requestVo, String... content) {
        switch (errorType) {
            case PORXY_NO_LONGER_SERVE:
                ErrorHelper.throwProxyServerNoLongerServeError(ctx.channel(), requestVo);
            case REQUEST_DATA_ERROR:
                ErrorHelper.throwNotFullRequestError(ctx.channel(), requestVo);
            case RESPONSE_DATA_ERROR:
                ErrorHelper.throwNotFullReponseError(ctx.channel(), requestVo);
            case HTTP_DATA_ERROR:
                ErrorHelper.throwNotFullHttpMessageError(ctx.channel(), requestVo);
            case URI_NO_PASS:
                ErrorHelper.throwUriError(ctx.channel(), requestVo);
        }
    }
}
